//*****************************************************************************
//
//								GridLine.c
//
//*****************************************************************************
//
//
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "printf.h"
#include "util.h"
#include "CPU.h"
#include "I2C.h"
#include "OSD\md_OSD.h"
#include "OSD\dv_OSD.h"
#include "OSD\app_OSD.h"
#include "SPI.h"
#include "Global.h"
#include "GridLine.h"
#include "app_MSG.h"
//#include "SPI_Map.h"

#include "GridLine_Inf.c"


#if 0   //Rose picture specification
FAR CONST struct _RLE_HEADER AniRoseAll_RLE_Header[] = 
{
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 126825L, 0 },
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 126919L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 127165L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 126960L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 126716L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 127079L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 126782L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 127034L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 127074L, 0 },  
	{ 0x54, 0x57, 0x88, 0x82, 400, 400, 127224L, 0 }
};  
#endif

#define FLASH_DATA_START_ADDR 	   0x7C7C40 //0x900000
CONST BITMAP SystemIcon1[]={
    {400,400,8,0,0,0,0x82,126825,0,(FLASH_DATA_START_ADDR+0x717)},/* Rose001_g1.bmp */
    {400,400,8,0,0,0,0x82,126919,1,(FLASH_DATA_START_ADDR+0x1f681)},/* Rose002_g1.bmp */
    {400,400,8,0,0,0,0x82,127165,2,(FLASH_DATA_START_ADDR+0x3e649)},/* Rose003_g1.bmp */
    {400,400,8,0,0,0,0x82,126960,3,(FLASH_DATA_START_ADDR+0x5d707)},/* Rose004_g1.bmp */
    {400,400,8,0,0,0,0x82,126716,4,(FLASH_DATA_START_ADDR+0x7c6f7)},/* Rose005_g1.bmp */
    {400,400,8,0,0,0,0x82,127079,5,(FLASH_DATA_START_ADDR+0x9b5f3)},/* Rose006_g1.bmp */
    {400,400,8,0,0,0,0x82,126782,6,(FLASH_DATA_START_ADDR+0xba65b)},/* Rose007_g1.bmp */
    {400,400,8,0,0,0,0x82,127034,7,(FLASH_DATA_START_ADDR+0xd9599)},/* Rose008_g1.bmp */
    {400,400,8,0,0,0,0x82,127074,8,(FLASH_DATA_START_ADDR+0xf85d3)},/* Rose009_g1.bmp */
    {400,400,8,0,0,0,0x82,127224,9,(FLASH_DATA_START_ADDR+0x117635)},/* Rose010_g1.bmp */
};


FAR CONST BYTE AniRoseAll_LUT[] = {    // LUT
    // Color Look Up Table (1024) bytes
    0x00,0x03,0x00,0x00,0x01,0x08,0x0a,0x00,0x1e,0x02,0x00,0x00,0x10,0x08,0x0a,0x00,
    0x00,0x12,0x00,0x00,0x07,0x11,0x0e,0x00,0x11,0x18,0x04,0x00,0x13,0x17,0x13,0x00,
    0x10,0x1c,0x1f,0x00,0x33,0x04,0x01,0x00,0x2e,0x1b,0x13,0x00,0x07,0x2b,0x07,0x00,
    0x2a,0x2b,0x0e,0x00,0x17,0x2a,0x20,0x00,0x2a,0x33,0x28,0x00,0x33,0x3c,0x3f,0x00,
    0x4e,0x05,0x01,0x00,0x58,0x07,0x15,0x00,0x6d,0x0b,0x02,0x00,0x72,0x18,0x15,0x00,
    0x51,0x22,0x08,0x00,0x72,0x28,0x05,0x00,0x6c,0x36,0x1f,0x00,0x51,0x34,0x21,0x00,
    0x17,0x41,0x0f,0x00,0x1f,0x5d,0x14,0x00,0x27,0x4c,0x13,0x00,0x30,0x61,0x17,0x00,
    0x1b,0x44,0x24,0x00,0x2c,0x4f,0x29,0x00,0x34,0x6b,0x2d,0x00,0x3b,0x7e,0x34,0x00,
    0x4c,0x46,0x1c,0x00,0x73,0x40,0x17,0x00,0x47,0x76,0x19,0x00,0x4e,0x4d,0x2f,0x00,
    0x71,0x4a,0x2c,0x00,0x48,0x70,0x30,0x00,0x67,0x6d,0x21,0x00,0x6e,0x71,0x35,0x00,
    0x46,0x3f,0x41,0x00,0x37,0x54,0x41,0x00,0x4a,0x51,0x4b,0x00,0x53,0x59,0x5c,0x00,
    0x71,0x56,0x42,0x00,0x4f,0x6c,0x4b,0x00,0x65,0x60,0x5e,0x00,0x73,0x67,0x52,0x00,
    0x5a,0x68,0x68,0x00,0x6f,0x71,0x6f,0x00,0x88,0x11,0x02,0x00,0x91,0x0e,0x1d,0x00,
    0xa4,0x19,0x03,0x00,0xaa,0x1f,0x17,0x00,0x90,0x2a,0x04,0x00,0x8e,0x35,0x1a,0x00,
    0xa8,0x32,0x03,0x00,0xb0,0x33,0x1d,0x00,0xc5,0x39,0x10,0x00,0x94,0x47,0x0c,0x00,
    0x9f,0x5f,0x16,0x00,0xb0,0x49,0x05,0x00,0xb3,0x63,0x12,0x00,0x8f,0x51,0x2a,0x00,
    0x86,0x40,0x37,0x00,0xad,0x51,0x26,0x00,0x95,0x66,0x32,0x00,0xb2,0x70,0x29,0x00,
    0xc6,0x54,0x0c,0x00,0xe4,0x54,0x07,0x00,0xcd,0x6a,0x13,0x00,0xe0,0x6e,0x18,0x00,
    0xcd,0x4e,0x27,0x00,0xe3,0x5d,0x2a,0x00,0xd1,0x76,0x28,0x00,0xe6,0x73,0x31,0x00,
    0x8a,0x58,0x40,0x00,0xa9,0x5b,0x42,0x00,0xa5,0x5b,0x59,0x00,0x86,0x66,0x44,0x00,
    0x9a,0x68,0x45,0x00,0x86,0x77,0x42,0x00,0x9a,0x76,0x44,0x00,0x87,0x69,0x58,0x00,
    0x98,0x69,0x57,0x00,0x87,0x74,0x5a,0x00,0x99,0x77,0x5a,0x00,0xad,0x75,0x4a,0x00,
    0x86,0x5f,0x61,0x00,0x8d,0x79,0x6b,0x00,0x8b,0x7b,0x7c,0x00,0xa7,0x7a,0x65,0x00,
    0xcf,0x75,0x4c,0x00,0x56,0x87,0x38,0x00,0x3e,0x83,0x48,0x00,0x66,0x8d,0x55,0x00,
    0x7d,0x99,0x5f,0x00,0xb8,0x81,0x37,0x00,0xdd,0x8a,0x2d,0x00,0x90,0x89,0x49,0x00,
    0xb2,0x89,0x51,0x00,0x8d,0xa3,0x50,0x00,0xad,0xa8,0x56,0x00,0x89,0x85,0x69,0x00,
    0x99,0x85,0x68,0x00,0x99,0x97,0x63,0x00,0x88,0x85,0x78,0x00,0x98,0x88,0x77,0x00,
    0x80,0x98,0x78,0x00,0x99,0x94,0x79,0x00,0xa8,0x87,0x65,0x00,0xbc,0x88,0x64,0x00,
    0xac,0x96,0x66,0x00,0xba,0x97,0x62,0x00,0xa8,0x8a,0x77,0x00,0xbb,0x8a,0x76,0x00,
    0xaa,0x96,0x78,0x00,0xb8,0x98,0x77,0x00,0x92,0xad,0x70,0x00,0xbd,0xa6,0x74,0x00,
    0xa4,0xbd,0x7d,0x00,0xcd,0x90,0x4c,0x00,0xec,0x96,0x41,0x00,0xd6,0xa2,0x58,0x00,
    0xf1,0xaa,0x4f,0x00,0xcc,0x97,0x6a,0x00,0xea,0x96,0x65,0x00,0xd5,0xab,0x72,0x00,
    0xf0,0xb6,0x6b,0x00,0xf4,0xc7,0x7a,0x00,0x73,0x7b,0x80,0x00,0x78,0x86,0x8b,0x00,
    0x88,0x89,0x87,0x00,0x97,0x8b,0x86,0x00,0x88,0x96,0x89,0x00,0x99,0x95,0x89,0x00,
    0x84,0x8b,0x98,0x00,0x96,0x8c,0x95,0x00,0x88,0x95,0x9a,0x00,0x98,0x99,0x97,0x00,
    0xa7,0x8b,0x86,0x00,0xba,0x8b,0x86,0x00,0xaa,0x97,0x88,0x00,0xb7,0x9a,0x88,0x00,
    0xab,0x8c,0x90,0x00,0xb9,0x8b,0x97,0x00,0xa7,0x9b,0x93,0x00,0xab,0x9c,0x9f,0x00,
    0xb7,0x9b,0x96,0x00,0x98,0xa6,0x99,0x00,0xac,0xa6,0x89,0x00,0xb9,0xa8,0x89,0x00,
    0xbc,0xb6,0x88,0x00,0xaa,0xa6,0x99,0x00,0xb9,0xa9,0x98,0x00,0xa0,0xb9,0x9d,0x00,
    0xbb,0xb6,0x99,0x00,0x91,0x9b,0xa6,0x00,0x99,0xa8,0xad,0x00,0xa8,0xab,0xa8,0x00,
    0xb8,0xac,0xa8,0x00,0xa9,0xb6,0xab,0x00,0xba,0xb7,0xaa,0x00,0xa5,0xad,0xb8,0x00,
    0xb7,0xac,0xb6,0x00,0xa7,0xb6,0xba,0x00,0xb9,0xba,0xb9,0x00,0xca,0x9a,0x84,0x00,
    0xe7,0x9c,0x86,0x00,0xc9,0xa9,0x86,0x00,0xd9,0xaa,0x88,0x00,0xcc,0xb6,0x89,0x00,
    0xda,0xb8,0x84,0x00,0xc8,0xab,0x98,0x00,0xda,0xab,0x97,0x00,0xca,0xb7,0x99,0x00,
    0xd9,0xba,0x98,0x00,0xee,0xb6,0x8d,0x00,0xc3,0x9b,0xa2,0x00,0xc8,0xac,0xa7,0x00,
    0xdc,0xac,0xa7,0x00,0xca,0xb9,0xa9,0x00,0xd9,0xbb,0xa9,0x00,0xc8,0xad,0xb8,0x00,
    0xc9,0xbc,0xb7,0x00,0xd9,0xbc,0xb8,0x00,0xeb,0xbb,0xa7,0x00,0xbb,0xc7,0xba,0x00,
    0xdb,0xc7,0x96,0x00,0xf4,0xce,0x8e,0x00,0xff,0xe9,0x97,0x00,0xcb,0xc7,0xab,0x00,
    0xdb,0xc8,0xaa,0x00,0xdc,0xd7,0xa8,0x00,0xcb,0xc8,0xba,0x00,0xda,0xca,0xb9,0x00,
    0xc9,0xd6,0xbb,0x00,0xdb,0xd7,0xba,0x00,0xe9,0xca,0xa8,0x00,0xfc,0xc9,0xa5,0x00,
    0xeb,0xd7,0xa9,0x00,0xfa,0xdb,0xa5,0x00,0xe9,0xcc,0xb8,0x00,0xfb,0xcb,0xb8,0x00,
    0xeb,0xd8,0xba,0x00,0xfa,0xdb,0xba,0x00,0xe3,0xe7,0xbd,0x00,0xf8,0xe9,0xb7,0x00,
    0xaf,0xbc,0xc6,0x00,0xc1,0xbd,0xc8,0x00,0x9f,0xc4,0xc7,0x00,0xb9,0xc9,0xce,0x00,
    0xbb,0xe0,0xdd,0x00,0xbf,0xd6,0xe5,0x00,0xca,0xcb,0xca,0x00,0xd9,0xcd,0xc9,0x00,
    0xcb,0xd7,0xcd,0x00,0xdb,0xd8,0xcb,0x00,0xc7,0xcd,0xd9,0x00,0xd8,0xcd,0xd7,0x00,
    0xcb,0xd8,0xda,0x00,0xda,0xdb,0xd9,0x00,0xe8,0xcd,0xc8,0x00,0xfd,0xcc,0xca,0x00,
    0xea,0xda,0xca,0x00,0xf9,0xdc,0xca,0x00,0xea,0xce,0xd7,0x00,0xff,0xcc,0xda,0x00,
    0xea,0xdd,0xd8,0x00,0xfa,0xdd,0xda,0x00,0xdb,0xe7,0xdb,0x00,0xec,0xe8,0xcc,0x00,
    0xfb,0xea,0xcb,0x00,0xef,0xf7,0xc8,0x00,0xfe,0xfa,0xc7,0x00,0xeb,0xe9,0xdb,0x00,
    0xfa,0xeb,0xda,0x00,0xee,0xf7,0xdb,0x00,0xfe,0xfa,0xda,0x00,0xd0,0xdd,0xe9,0x00,
    0xe1,0xde,0xe9,0x00,0xcc,0xe7,0xe9,0x00,0xdb,0xea,0xeb,0x00,0xdd,0xf7,0xef,0x00,
    0xcd,0xec,0xf6,0x00,0xdb,0xed,0xf8,0x00,0xdd,0xf7,0xf9,0x00,0xea,0xeb,0xea,0x00,
    0xf9,0xee,0xea,0x00,0xec,0xf7,0xed,0x00,0xfb,0xf9,0xed,0x00,0xed,0xef,0xf4,0x00,
    0xf9,0xef,0xf9,0x00,0xec,0xf9,0xfa,0x00,0xfb,0xfc,0xfb,0x00,0xff,0xff,0xff,0x00, }; 
 



CONST ATTRIBUTE canvas0_attr1[]={
{0x01, 0x02, 0x00, 0, 0},// color alpha blending
{0x02, 0x00, 0x00, 0, 0},//color blinking
{0xff,0,0,0}
};


CANVAS User_Canvas1[]={
    {0, 0, 0, 0, 0, 8, 800, 480, canvas0_attr1, 0},
	{0, 480, 0, 0, 2, 16, 800, 480, canvas0_attr1, 2},
};


//---------- Slide Demo using 16bit OSD -------------------

void GridLineInit( void )
{
//DWORD ddrS=GRID_DDR_START, spiS=GRID_SPI_START;
//BYTE	lut[64], i;  //Stone del
//BYTE	buffer[8];
//for ( i=0; i<64; i++ ) {
//		lut[i] = bmp_GRID_LUT[i];
//	}
//	OSD_LoadLUT( 0,4, lut );
	OSD_LoadLUT( 0,4, bmp_GRID_LUT );
	TWLL_OSDLayer_Init();	
	TWLL_Canvas_Init(&User_Canvas1[0]);
	OSD_BlockFill(0,0,800,480,0,8);

}

//--------------------------------------------------------------
//
//--------------------------------------------------------------
void GridLineDraw( BYTE n )
{
//DWORD ddrS=GRID_DDR_START, spiS=GRID_SPI_START;
//BYTE	buffer[8];

	BITMAP pBMP;
//	spiS = GridLine[n][0];
	//Printf("\n\r Spis=%6lx\n", spiS);

   //OSD_SetBitmapColorWriteMode( 0|0x30|0|0x04); //CPUWRITE | BLT_NO | CONV_DIS | PIXEL8 );

	//Printf("\n\r loading address =%6lx ", spiS+1032);
	pBMP.width=800;
	pBMP.height=430;
	pBMP.compression=0x89;
	pBMP.color_depth=0x88;
	pBMP.data_length=GridLine[n][1];
	pBMP.bmpdata=GridLine[n][0]+1032;
	//Printf("\n\r %bx , %bx, 0x%8lx, 0x%8lx\n",pBMP.compression,pBMP.color_depth,pBMP.data_length,pBMP.bmpdata );
	

	//Printf("\n\r color depth %bd ,compression %bx,datalength 0x%8lx, address 0x%8lx\n",pBMP->color_depth,pBMP->compression,pBMP->data_length,pBMP->bmpdata );
    OSD_LoadBmp((const U8 *)&pBMP, 0, 0, 8);

   //	OsdWriteMode( BLOCKTRANS | BLT_NO | CONV_DIS | PIXEL8 );

	//OSD_BlockTransfer( 0, 480,  800, 480, 0, 0,8);

}

void GridLineDemo( void )
{
	static BYTE current;
	CMDST touchcmd;
	BOOL ret;
	OSD_ShowWindowBmp(0, 0);					// OFF the display
	OSD_ShowWindowBmp(2, 0);
	GridLineInit();
	current=0;
	GridLineDraw(current);
	OSD_ShowWindowBmp(0, 1);
	while(1)
		{
		ret=CheckTSCCMD(&touchcmd);
		if(ret)
			{
			//	Printf("\n\rtype= %x , buf1=%x, buf2=%x\n",touchcmd.Cmdbuf[0],touchcmd.Cmdbuf[1],touchcmd.Cmdbuf[2]);
				if((touchcmd.Cmdbuf[0]==0)&& (touchcmd.Cmdbuf[1]>700)&&((touchcmd.Cmdbuf[2]&0x1fff)>400))
					{
						//Printf("\nEnter Break\n");
						goto RET;
					}
				if((touchcmd.Cmdbuf[1]>touchcmd.Cmdbuf[3])&&(touchcmd.Cmdbuf[1]-touchcmd.Cmdbuf[3])>50)
				{	
					if(current>0)
					current--;
					GridLineDraw(current);
				}
				else if((touchcmd.Cmdbuf[1]<touchcmd.Cmdbuf[3])&&(touchcmd.Cmdbuf[3]-touchcmd.Cmdbuf[1])>50)
				{
					current++;
		    		if(current>39)
						current=39;
					GridLineDraw(current);
				}
				
				
			}
		}
	RET:  
		//Printf("\n\r Exit Rose Demo\n");
		OSD_ShowWindowBmp(0, 0);
	//OSD_ShowWindowBmp(1, 1);
	//OSD_ShowWindowBmp(2, 1);
	
}

static void OsdWaitVBlank(BYTE cnt)
{
	BYTE i;
	for(i=0; i<cnt; i++) {
		OSD_WaitDisplayBlank(1);
	}
}


void RoseDemo( void )
{
	BYTE ret=1/*, AutoKey, _RemoDataCode, GoSub = 0*/;
	BYTE i/*, ts*/;
	//BYTE duration=6;
	//WORD	xpos, ypos;
	//BITMAP pBMP;
	//int ilut;
	static BYTE current=1;
	CMDST touchcmd;
	
	//Printf("\nStarting Rose mode");
	//Printf("\n\n\n");
	OSD_ShowWindowBmp(0, 0);				// enable only 8bit
	OSD_ShowWindowBmp(1, 0);				// enable only 8bit
	OSD_ShowWindowBmp(2, 0);				// enable only 8bit

	TWLL_OSDLayer_Init();	
	TWLL_Canvas_Init(&User_Canvas1[0]);
	OSD_BlockFill(0,0,800,480,0,8);

	OSD_LoadLUT( 0,8, AniRoseAll_LUT );
	OSD_ShowWindowBmp(0, 1);				// enable only 8bit

	while(1) {
			for(i=0; i<10; i++) {
				OSD_LoadBmp((const U8 *)&SystemIcon1[i], 200, 83, 8);
				OsdWaitVBlank(current);
			ret=CheckTSCCMD(&touchcmd);
			if(ret)
			{
			
			//	Printf("\n\rtype= %x , buf1=%x, buf2=%x\n",touchcmd.Cmdbuf[0],touchcmd.Cmdbuf[1],touchcmd.Cmdbuf[2]);
				if((touchcmd.Cmdbuf[0]==0)&& (touchcmd.Cmdbuf[1]>700)&&((touchcmd.Cmdbuf[2]&0x1fff)>400))
					{
			//			Printf("\nEnter Break\n");
						goto RET;
					}
				if((touchcmd.Cmdbuf[1]>touchcmd.Cmdbuf[3])&&(touchcmd.Cmdbuf[1]-touchcmd.Cmdbuf[3])>50)
				{	
					if(current>1)
						current--;
					else
						current=0;
				//	Printf("\n\r Current=%bx\n", current);
				}
				else if((touchcmd.Cmdbuf[3]>touchcmd.Cmdbuf[1])&&(touchcmd.Cmdbuf[3]-touchcmd.Cmdbuf[1])>50)
				{
					if(current<10)
						current++;
		    		else
						current=10;
				//		Printf("\n\r Current1=%bx\n", current);
				}
				
			}
			}
		}

RET:  
	//Printf("\n\r Exit Rose Demo\n");
	OSD_ShowWindowBmp(0, 0);
	
}

#if 0
void SlideShowDemo(void)
{
	BYTE ret=1;
	CMDST touchcmd;
	static BYTE current=0;
	static BOOL flag=FALSE;

	TWLL_OSDLayer_Init();	
	TWLL_Canvas_Init(&User_Canvas1[1]);
	OSD_ShowWindowBmp(2, 1);				// enable only 16bit
	OSD_LoadBmp((const U8 *)&SystemIcon2[current], 0, 480, 16);
	while(1)
		{
			ret=CheckTSCCMD(&touchcmd);
			if(touchcmd.Cmdbuf[0]==0)
			flag=TRUE;
			if((ret&flag)&(touchcmd.Cmdbuf[0]==5))
			{
			
				Printf("\n\r Capture Moving Action\n");
				if(((touchcmd.Cmdbuf[1]-touchcmd.Cmdbuf[3])>50)&(touchcmd.Cmdbuf[1]>touchcmd.Cmdbuf[3]))
				{	
					if(current>0)
						current--;
					else
						current=0;
					OSD_LoadBmp((const U8 *)&SystemIcon2[current], 0, 480, 16);
					flag=FALSE;
					//OsdWaitVBlank(2);
				}
				else if(((touchcmd.Cmdbuf[3]-touchcmd.Cmdbuf[1])>50)&(touchcmd.Cmdbuf[3]>touchcmd.Cmdbuf[1]))
				{
					if(current<3)
						current++;
		    		else
						current=3;
					
					OSD_LoadBmp((const U8 *)&SystemIcon2[current], 0, 480, 16);
					flag=FALSE;
					//OsdWaitVBlank(2);
				}
				if((touchcmd.cmdtype=0)&& (touchcmd.Cmdbuf[1]<100))
					break;
			}

			

	}
	

	
}

#endif

